<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="发帖人" prop="dictName">
        <el-input
          v-model="queryParams.dictName"
          placeholder="请输入发帖人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="舆情内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入舆情内容"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sywx:yq:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sywx:yq:export']"
        >导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-s-data"
          size="mini"
          @click="dataAnalysis"
          v-hasPermi="['sywx:yq:export']"
        >数据分析</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号"
                       align="center"
                       width="50">
        <template slot-scope="scope">
          <span>{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日期" align="center" prop="occurDate" />
      <el-table-column label="舆情等级" align="center" prop="level" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sywx_yq_level" :value="scope.row.level" />
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sywx_yq_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="舆情内容" align="center" prop="content" :show-overflow-tooltip="true" />
      <el-table-column label="链接" align="center" prop="url" :show-overflow-tooltip="true" />
      <el-table-column label="涉情单位" align="center" prop="unitName" :show-overflow-tooltip="true" />
      <el-table-column label="附件" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link
            v-if="scope.row.annexNum>0"
            :to="'/system/dict-data/index/' + scope.row.annex"
            class="link-type"
          >
            <span>{{ scope.row.annexNum }}</span>
          </router-link>
          <span v-if="scope.row.annexNum==0">{{ scope.row.annexNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="反馈" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link
            v-if="scope.row.feedbackNum>0"
            :to="'/system/dict-data/index/' + scope.row.feedback"
            class="link-type"
          >
            <span>{{ scope.row.feedbackNum }}</span>
          </router-link>
          <span v-if="scope.row.feedbackNum==0">{{ scope.row.feedbackNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sywx:yq:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sywx:yq:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="日期" prop="occurDate">
              <el-date-picker v-model="form.occurDate" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="舆情等级" prop="level">
              <el-select v-model="form.level" placeholder="舆情等级" clearable>
                <el-option
                  v-for="dict in dict.type.sywx_yq_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类" prop="type">
              <el-select v-model="form.type" placeholder="舆情分类" clearable>
                <el-option
                  v-for="dict in dict.type.sywx_yq_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="舆情内容" prop="content">
              <el-input v-model="form.content" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="舆情链接" prop="url">
              <el-input v-model="form.url" type="textarea" placeholder="请输入舆情链接"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="发帖人" prop="yqPoster">
              <el-transfer
                filterable
                :filter-method="posterFilterMethod"
                :titles="['请选择', '已选']"
                filter-placeholder="请选择发帖人"
                v-model="form.yqPoster"
                :props="{
                  key: 'id',
                  label: 'netName'
                }"
                :data="posters"
              ></el-transfer>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="涉情单位" prop="yqUnit">
              <el-transfer
                filterable
                :filter-method="filterMethod"
                :titles="['请选择', '已选']"
                filter-placeholder="请选择涉情单位"
                v-model="form.yqUnit"
                :props="{
                  key: 'id',
                  label: 'unitName'
                }"
                :data="units"
              ></el-transfer>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件" prop="annex">
              <FileUpload v-model="form.annex" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="反馈" prop="feedback">
              <fileUpload v-model="form.feedback" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 数据分析 -->
    <el-dialog title="数据分析" :visible.sync="dataOpen" width="780px" append-to-body>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dataOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, get, del, add, update } from "@/api/sywx/yqdj";
import { listsPost } from "@/api/sywx/unitInfo";
import { lists as posterLists } from "@/api/sywx/poster";
export default {
  name: "Dict",
  dicts: ["sywx_yq_level", "sywx_yq_type"],
  data() {
    return {
      units: [],
      posters: [],
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dataOpen: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        occurDate: undefined,
        content: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        occurDate: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "舆情等级不能为空", trigger: "blur" }
        ],
        type: [{ required: true, message: "分类不能为空", trigger: "blur" }],
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
        url: [{ required: true, message: "链接不能为空", trigger: "blur" }],
        yqUnit: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个涉情单位",
            trigger: "change"
          }
        ]
      },
      annexType: [
        {
          value: 1,
          lable: "图片"
        },
        {
          value: 2,
          lable: "视频"
        }
      ]
    };
  },
  created() {
    this.getLists();
    this.getPosterLists()
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询所有列表 */
    getLists() {
      listsPost().then(response => {
        this.units = response;
      });
    },
    /** 查询发帖人所有列表 */
    getPosterLists() {
      posterLists().then(response => {
        this.posters = response;
      });
    },
    filterMethod(query, item) {
      return item.unitName.indexOf(query) > -1;
    },
    posterFilterMethod(query, item) {
      return item.netName.indexOf(query) > -1;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined,
        yqUnit: undefined,
        yqPoster: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      get(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字典类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除该数据项？")
        .then(function() {
          return del(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    dataAnalysis() {
      this.dataOpen = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dict/type/export",
        {
          ...this.queryParams
        },
        `type_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
